﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Brainee
{
  public static class Extensions
  {
    public static void SafeUICall<T> (this T control, Action<T> action) where T : Control
    {
      if (control.InvokeRequired)
      {
        // It's on a different thread, so use Invoke.
        //control.Invoke(new SafeUICallDelegate<T>(SafeUICall), new object[] { control, action });
        MethodInvoker mi = () => SafeUICall (control, action);
        control.Invoke (mi);
        return;
      }

      // It's on the same thread, no need for Invoke
      action (control);
    }

    public static void SafeText<T> (this T control, string text) where T : Control
    {
      control.SafeUICall (c => c.Text = text);
    }
  }
}
